package com.dayuanit.dy17.atm.service.impl;

import com.dayuanit.dy17.atm.entity.Card;
import com.dayuanit.dy17.atm.entity.Flow;
import com.dayuanit.dy17.atm.entity.TransferOrder;
import com.dayuanit.dy17.atm.enums.FlowTypeEnum;
import com.dayuanit.dy17.atm.enums.TransferOrderStatusEnum;
import com.dayuanit.dy17.atm.exception.BizException;
import com.dayuanit.dy17.atm.mapper.CardMapper;
import com.dayuanit.dy17.atm.mapper.FlowMapper;
import com.dayuanit.dy17.atm.mapper.TransferOrderMapper;
import com.dayuanit.dy17.atm.service.ITransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TransferServiceImpl implements ITransferService {

    @Autowired
    private TransferOrderMapper transferOrderMapper;

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private FlowMapper flowMapper;

    @Override
    public List<TransferOrder> loadDelayTransferOrder(TransferOrderStatusEnum transferOrderStatusEnum) {
        List<TransferOrder> transferOrders = transferOrderMapper.listDelayOrders(transferOrderStatusEnum.getK());
        return transferOrders;
    }

    @Transactional
    @Override
    public void transferIn(TransferOrder order) {
        Card cardIn = cardMapper.getByNumber(order.getCardInNum());
        cardIn.setBalance(cardIn.getBalance() + order.getAmount());

        //修改余额
        int rows = cardMapper.updateByPrimaryKey(cardIn);
        if (1 != rows) {
            throw new BizException("异步转账失败");
        }

        //增加流水
        Flow flow = new Flow();
        flow.setUserId(cardIn.getUserId());
        flow.setRemark("转账收入");
        flow.setFlowType(FlowTypeEnum.transfer_in.getK());
        flow.setCreateTime(new Date());
        flow.setCardId(cardIn.getId());
        flow.setAmount(order.getAmount());
        rows = flowMapper.insert(flow);
        if (1 != rows) {
            throw new BizException("异步转账失败");
        }

        // 修改转账订单状态
        order.setStatus(TransferOrderStatusEnum.success_transfer.getK());
        order.setModifyTime(new Date());

        rows = transferOrderMapper.updateByPrimaryKey(order);
        if (1 != rows) {
            throw new BizException("异步转账失败");
        }
    }

    @Transactional
    @Override
    public void transferRollback(TransferOrder order) {
        // 把钱给转出账号
        Card cardOut = cardMapper.selectByPrimaryKey(order.getCardIdOut());
        cardOut.setBalance(cardOut.getBalance() + order.getAmount());

        int rows = cardMapper.updateByPrimaryKey(cardOut);
        if (1 != rows) {
            throw new BizException("异步转账退款失败");
        }

        Flow flow = new Flow();
        flow.setCardId(cardOut.getId());
        flow.setCreateTime(new Date());
        flow.setFlowType(FlowTypeEnum.transfer_back.getK());
        flow.setRemark("转账退款");
        flow.setUserId(cardOut.getUserId());
        flow.setAmount(order.getAmount());

        rows = flowMapper.insert(flow);
        if (1 != rows) {
            throw new BizException("异步转账退款失败");
        }

        //修改转账订单状态
        order.setStatus(TransferOrderStatusEnum.cancal_transfer.getK());
        rows = transferOrderMapper.updateByPrimaryKey(order);
        if (1 != rows) {
            throw new BizException("异步转账退款失败");
        }
    }

    @Override
    public void changeOrderStatus(TransferOrder order, TransferOrderStatusEnum orderStatusEnum) {
        order.setStatus(orderStatusEnum.getK());
        int rows = transferOrderMapper.updateByPrimaryKey(order);
        if (1 != rows) {
            throw new BizException("转账订单修改失败");
        }
    }
}
